package com.jp.java8;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class SimpleStream {

    public static void main(String[] args) {

        List<Dish> list = Arrays.asList(new Dish("pork",false,800,Dish.Type.MEAT),
                new Dish("beef",false,700,Dish.Type.MEAT),
                new Dish("chicken",false,400,Dish.Type.MEAT),
                new Dish("french fries",true,530,Dish.Type.OTHER),
                new Dish("rice",true,350,Dish.Type.OTHER),
                new Dish("senson fruit",true,120,Dish.Type.OTHER),
                new Dish("pizza",true,550,Dish.Type.OTHER),
                new Dish("prawns",false,300,Dish.Type.FISH),
                new Dish("salmon",false,450,Dish.Type.FISH));
        List<String> dishNamesByCollections = getDishNamesByCollections(list);
        System.out.println(dishNamesByCollections);

        List<String> dishNamesByStream = getDishNamesByStream(list);
        System.out.println(dishNamesByStream);


        Stream<Dish> stream = list.stream();
        stream.forEach(System.out::println);
//        stream.forEach(System.out::println);


        List<String> result = list.stream().filter(d->{
                        System.out.println("filter->"+d.getName());
                        return  d.getCalories()<=400;
                    })
                .map(d -> {
                    System.out.println("map->"+d.getName());
                    return d.getName();
                })
                .limit(3).collect(toList());


    }

    private static List<String> getDishNamesByStream(List<Dish> menu){
        return  menu.stream().filter((d)->d.getCalories()<=400)
                .sorted(Comparator.comparing(Dish::getCalories))
                .map(Dish::getName)
                .collect(toList());
    }

    private static List<String>  getDishNamesByCollections(List<Dish> menu){
        List<Dish> lowCalories = new ArrayList<>();
        for (Dish dish : menu){
            if(dish.getCalories()<=400){
                lowCalories.add(dish);
            }
        }
        Collections.sort(lowCalories, Comparator.comparingInt(Dish::getCalories));
        List<String> names = new ArrayList<>();
        for(Dish d : lowCalories){
            names.add(d.getName());
        }
        return  names;
    }
}
